package com.ctg.behavior.calc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.calculator.CtLTVCalculator;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.ltv.CtLTVCalcRequest;
import com.ctg.behavior.common.ltv.CtLTVCalcResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/ltv")
public class CtLTVReportController {

	@Autowired
	private CtLTVCalculator calculator;

	@PostMapping("/report")
	public CtLTVCalcResult calcReport(@RequestBody CtLTVCalcRequest request) throws Exception {

		CtLTVCalcResult calcResult = new CtLTVCalcResult();
		log.info("body={}", JSON.toJSONString(request.getReq()));

		CtCalcResult result = calculator.doCalculator(request.getReq());
		log.info("calcResult={}", JSON.toJSONString(result));
		if (!(result instanceof CtLTVCalcResult)) {
			log.error("calcResult class={}", result.getClass());
			log.error("calcResult={}", JSON.toJSONString(result));
			return calcResult;
		}
		calcResult = (CtLTVCalcResult) result;
		return calcResult;

	}
	
}
